using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class UserGiftCardCategoryDal
	{
		public bool Exists(long id)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from MBF_UserGiftCardCategory");
			stringBuilder.Append(" where ");
			stringBuilder.Append("id = @id ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@id", SqlDbType.BigInt)
			};
			array[0].Value = id;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public long Add(UserGiftCardCategoryModel model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into MBF_UserGiftCardCategory(");
			stringBuilder.Append("FestivalName,Picture,Title");
			stringBuilder.Append(") values (");
			stringBuilder.Append("@FestivalName,@Picture,@Title");
			stringBuilder.Append(") ");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@FestivalName", SqlDbType.NVarChar, 50),
				new SqlParameter("@Picture", SqlDbType.NVarChar, 50),
				new SqlParameter("@Title", SqlDbType.NVarChar, 50)
			};
			array[0].Value = model.FestivalName;
			array[1].Value = model.Picture;
			array[2].Value = model.Title;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			long result;
			if (single == null)
			{
				result = 0L;
			}
			else
			{
				result = System.Convert.ToInt64(single);
			}
			return result;
		}
		public bool Update(UserGiftCardCategoryModel model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update MBF_UserGiftCardCategory set ");
			stringBuilder.Append(" FestivalName = @FestivalName , ");
			stringBuilder.Append(" Picture = @Picture , ");
			stringBuilder.Append(" Title = @Title  ");
			stringBuilder.Append(" where id=@id ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@id", SqlDbType.BigInt, 8),
				new SqlParameter("@FestivalName", SqlDbType.NVarChar, 50),
				new SqlParameter("@Picture", SqlDbType.NVarChar, 50),
				new SqlParameter("@Title", SqlDbType.NVarChar, 50)
			};
			array[0].Value = model.id;
			array[1].Value = model.FestivalName;
			array[2].Value = model.Picture;
			array[3].Value = model.Title;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(long id)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from MBF_UserGiftCardCategory ");
			stringBuilder.Append(" where id=@id");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@id", SqlDbType.BigInt)
			};
			array[0].Value = id;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool DeleteList(string idlist)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from MBF_UserGiftCardCategory ");
			stringBuilder.Append(" where ID in (" + idlist + ")  ");
			int num = Conn.ExecuteSql(stringBuilder.ToString(), new IDbDataParameter[0]);
			return num > 0;
		}
		public UserGiftCardCategoryModel GetModel(long id)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select id, FestivalName, Picture, Title  ");
			stringBuilder.Append("  from MBF_UserGiftCardCategory ");
			stringBuilder.Append(" where id=@id");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@id", SqlDbType.BigInt)
			};
			array[0].Value = id;
			UserGiftCardCategoryModel userGiftCardCategoryModel = new UserGiftCardCategoryModel();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			UserGiftCardCategoryModel result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				if (dataSet.Tables[0].Rows[0]["id"] != null && dataSet.Tables[0].Rows[0]["id"].ToString() != "")
				{
					userGiftCardCategoryModel.id = new long?(long.Parse(dataSet.Tables[0].Rows[0]["id"].ToString()));
				}
				userGiftCardCategoryModel.FestivalName = dataSet.Tables[0].Rows[0]["FestivalName"].ToString();
				userGiftCardCategoryModel.Picture = dataSet.Tables[0].Rows[0]["Picture"].ToString();
				userGiftCardCategoryModel.Title = dataSet.Tables[0].Rows[0]["Title"].ToString();
				result = userGiftCardCategoryModel;
			}
			else
			{
				result = null;
			}
			return result;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select * ");
			stringBuilder.Append(" FROM MBF_UserGiftCardCategory ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string fieldOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" * ");
			stringBuilder.Append(" FROM MBF_UserGiftCardCategory ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + fieldOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetListRowid(int Top, string strWhere, string fieldOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" ROW_NUMBER() OVER (ORDER BY id ASC) rowid,* ");
			stringBuilder.Append(" FROM MBF_UserGiftCardCategory ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + fieldOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetPagedList(int PageIndex, int PageSize, string strWhere, string Order)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.AppendFormat("select  top {0} * from [MBF_UserGiftCardCategory] where ", PageSize);
			if (strWhere.Trim() != "")
			{
				stringBuilder.AppendFormat(" {0} and ", strWhere);
			}
			stringBuilder.AppendFormat(" id not in (select top {0} id from [MBF_UserGiftCardCategory] ", (PageIndex - 1) * PageSize);
			if (strWhere.Trim() != "")
			{
				stringBuilder.AppendFormat(" where {0} ", strWhere);
			}
			if (Order.Trim() != "")
			{
				stringBuilder.Append(" order by  " + Order);
			}
			stringBuilder.Append(")");
			if (Order.Trim() != "")
			{
				stringBuilder.Append(" order by  " + Order);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
	}
}
